//导入封装好的请求
// const url = require('../../config/config.js ')
const url = require('../../config/config.js')
    // import url from '../../config/config.js'
Page({
    data: {
        goodsList: [],
        tabIndex: 4 + ''

    },
    // 0:已关闭，1:待货中，2:已开工，3:已完成
    totalPages: 0,

    pageVo: { size: 5, indexPageNum: 1 },
    orderAndVo: { state: 1 },

    //options(Object)
    onLoad: function() {
        const data = { pageVo: this.pageVo }
        this.getGoodList(data)
        console.log(url.b.orderGoodsList)

    },
    onShow() {
        //读取token，如果token不存在,直接不给予返回
        const token = wx.getStorageSync("token");
        console.log(token)
        if (!token) {
            wx.navigateTo({
                url: '/pages/login/index'
            });
        }
    },
    getGoodList(data) {
        const header = {
            'content-type': 'application/json',
            'Cookie': "JSESSIONID=" + wx.getStorageSync("token") //读取cookie
        }
        wx.request({
            url: url.b.orderGoodsList,
            data: data,
            header: header,
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: (result) => {
                try {
                    console.log(result)
                        // 获取总条数
                    const total = result.data.data.pageResult.totalCount
                        // 计算总页数
                    this.totalPages = Math.ceil(total / this.pageVo.size);
                    const { resultList } = result.data.data;
                    if (resultList.length > 0) {
                        wx.showLoading({
                            title: "加载中",
                            mask: true,
                            complete: () => {}
                        });
                        this.setData({
                            goodsList: result.data.data.resultList
                        })
                        wx.hideLoading();
                        // 结构赋值
                    } else {
                        wx.showToast({
                            title: '没有数据了',
                            icon: 'none',
                        });
                    }
                } catch (error) {
                    wx.showToast({
                        title: '登陆过期',
                        icon: 'none',
                    });
                    wx.removeStorageSync({ token: "token" });
                    wx.navigateTo({
                        url: '/pages/login/index',

                    });
                }
            },
            fail(err) {
                console.log(url)
                console.log(err);
                wx.showToast({
                    title: '请求失败，请稍后重试',
                    icon: 'none',
                });
            }
        });
    },
    // 获取分页数
    handleTapReduce() {
        if (this.pageVo.indexPageNum > 1) {
            this.pageVo.indexPageNum--;
            this.getGoodList()
        } else {
            wx.showToast({
                title: '没有上一页数据了',
                icon: "none"
            });
        }
    },
    handleTapAdd() {
        //如果当前页数大于等于总页数，则证明没有数据了 
        if (this.pageVo.indexPageNum >= this.totalPages) {
            wx.showToast({
                title: '没有数据了',
                icon: 'none',
            });
        } else {
            console.log(111);
            this.pageVo.indexPageNum++;
            wx.showLoading({
                title: "加载下一页数据中",
                mask: true
            });

            this.getGoodList();
            wx.hideLoading();
        }


    },
    // 点击修改样式
    handleTap(e) {
        //清空原来的数组
        this.setData({
                goodsList: []
            })
            //清空原来的数据
        this.pageVo.indexPageNum = 1;
        const { state } = e.currentTarget.dataset;
        console.log(state)
            // 修改tabIndex的状态
        this.setData({
            tabIndex: state
        })
        this.orderAndVo.state = state;
        this.state = state;
        this.judgeState();
    },
    //判断state状态
    judgeState() {
        if (this.state < 4) {
            const data = { pageVo: this.pageVo, orderAndVo: this.orderAndVo };
            this.getGoodList(data);
        } else {
            const data = { pageVo: this.pageVo, orderAndVo: { type: this.data.type } }
            this.getGoodList(data)
        }

    }

});